package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.base.Paking;
import com.suineg.pms.service.base.BuildingService;
import com.suineg.pms.service.base.PakingService;
import com.suineg.pms.util.StringUtils;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 车位保存 拦截器
 *@author Suine
 */
public class PakingIntercepter extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private BuildingService buildingService = AppConfig.getInstance().getWac().getBean(BuildingService.class);
	private PakingService service = AppConfig.getInstance().getWac().getBean(PakingService.class);

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		check(rrequest,mRowData);
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}

	/**
	 * 
	 * @param rrequest
	 * @param mRowData
	 */
	public void check(ReportRequest rrequest,Map<String, String> mRowData){
		String id = mRowData.get("id");
		String communityId = mRowData.get("community_id");
		String buildingId = mRowData.get("building_id");
		String location = mRowData.get("localtion");
		String no = mRowData.get("no");
		
		
		
		Paking paking = null;
		
		if(StringUtils.isEmpty(id)){		//新加
			
			checkNo(rrequest,no);
			checkLocation(rrequest,buildingId,location);
		}else{								//修改
			paking = service.get(Long.valueOf(id));
			
			if(!paking.getNo().equals(no)){    //是否修改编号 
				checkNo(rrequest,no);
			}
			
			if(!paking.getCommunity().getId().equals(Long.valueOf(communityId))){	//是否修改小区
				checkLocation(rrequest,buildingId,location);
			}
			
			if(!paking.getBuilding().getId().equals(Long.valueOf(buildingId))){		//是否修改楼栋
				checkLocation(rrequest,buildingId,location);
			}
			
		}
	}
	
	/**
	 * 判断 位置是否存在
	 * @param rrequest
	 * @param buildingId
	 * @param location
	 */
	public void checkLocation(ReportRequest rrequest,String buildingId,String location){
		List<Paking> pakingList = null;
		pakingList = buildingService.get(Long.valueOf(buildingId)).getPakingList();
		for(Paking n1 : pakingList){
			if(n1.getLocaltion().equals(location)){
				rrequest.getWResponse().getMessageCollector().warn("此位置已存在!", false, Consts.STATECODE_FAILED);
				return ;
			}
		}
	}
	
	/**
	 * 判断 编号 是否存在
	 * @param rrequest
	 * @param no
	 */
	public void checkNo(ReportRequest rrequest,String no){
		Paking pk = service.findByNo(no);
		if(pk != null){
			rrequest.getWResponse().getMessageCollector().warn("此车位编号已存在!", false, Consts.STATECODE_FAILED);
		}
	}
}

